<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>企业管理</el-breadcrumb-item>
      <el-breadcrumb-item>企业信息列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="企业编号/企业名称"
            class="input-with-select"
            v-model="queryInfo.querystring"
            clearable
            @clear="getcompanylist"
          >
          <el-input
  type="textarea"
  :rows="2"
  placeholder="请输入内容"
  v-model="textarea">
</el-input>
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getcompanylist"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加企业</el-button
          >
        </el-col>
      </el-row>
      <!-- 用户列表区域 -->
      <el-table :data="companylist" border stripe>
        <el-table-column type="expand" prop="profile">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="公司简历">
                <span v-html="props.row.profile"></span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="companyid" sortable label="企业编号">
        </el-table-column>
        <el-table-column prop="companyname" sortable label="企业名称">
        </el-table-column>
        <el-table-column prop="principal" sortable label="负责人">
        </el-table-column>
        <el-table-column prop="email" sortable label="邮箱"> </el-table-column>
        <el-table-column prop="phone" sortable label="电话"> </el-table-column>
        <!-- <el-table-column prop="profile" sortable label="企业介绍">
        </el-table-column> -->
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮-->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加用户的对话框 -->
    <el-dialog
      title="添加企业"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <!-- 内容主体区 -->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="企业编号" prop="companyid">
          <el-input v-model="addForm.companyid"></el-input>
        </el-form-item>
        <el-form-item label="企业名称" prop="companyname">
          <el-input v-model="addForm.companyname"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="principal">
          <el-input v-model="addForm.principal"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="企业介绍" prop="profile">
          <el-input v-model="addForm.profile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addcompany">确 定</el-button>
      </span>
    </el-dialog>
    <!--修改用户的对话框  -->
    <el-dialog
      title="修改企业"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="企业编号" prop="companyid">
          <el-input v-model="editForm.companyid" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="企业名称" prop="companyname">
          <el-input v-model="editForm.companyname"></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="principal">
          <el-input v-model="editForm.principal"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="企业介绍" prop="profile">
          <el-input v-model="editForm.profile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editcompanyInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    //验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      // 验证邮箱的正则表达式
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
      if (regEmail.test(value)) {
        // 合法的邮箱
        return cb()
      }
      cb(new Error('请输入合法的邮箱'))
    }
    // 验证手机号的规则
    var checkphone = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regphone =
        /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

      if (regphone.test(value)) {
        return cb()
      }

      cb(new Error('请输入合法的手机号'))
    }
    return {
      //获取用户列表的参数
      queryInfo: {
        querystring: '',
        pagenum: 1,
        pagesize: 2,
      },
      companylist: [],
      total: 0,
      //控制添加用户对话框的显示和隐藏
      addDialogVisible: false,
      //添加用户的表单数据
      addForm: {
        companyid: '',
        companyname: '',
        password: '',
        principal: '',
        email: '',
        phone: '',
        profile: '',
      },
      //添加表单的验证规则对象
      addFormRules: {
        companyid: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '企业账号的长度在3~10之间',
            trigger: 'blur',
          },
        ],
        companyname: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 1,
            max: 10,
            message: '用户名的长度在1~10之间',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '密码的长度在6~15之间', trigger: 'blur' },
        ],
        principal: [
          { required: true, message: '请输入负责人', trigger: 'blur' },
          {
            min: 2,
            max: 15,
            message: '负责人的长度在6~15之间',
            trigger: 'blur',
          },
        ],

        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '请输入手机', trigger: 'blur' },
          { validator: checkphone, trigger: 'blur' },
        ],
        profile: [
          { required: true, message: '请输入企业介绍', trigger: 'blur' },
          {
            min: 2,
            max: 100,
            message: '企业介绍的长度在2~100之间',
            trigger: 'blur',
          },
        ],
      },
      //控制修改用户对话框的显示与隐藏
      editDialogVisible: false,
      //查询到的用户信息对象
      editForm: {},
      //修改表单的验证规则
      editFormRules: {
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '请输入手机', trigger: 'blur' },
          { validator: checkphone, trigger: 'blur' },
        ],
      },
    }
  },
  created() {
    this.getcompanylist()
  },
  methods: {
    async getcompanylist() {
      const { data: res } = await this.$http.get('company', {
        params: this.queryInfo,
      })
      if (res.meta.status != 200) return this.$message.error(res.meta.msg)
      this.companylist = res.data.records
      this.total = res.data.total
    },
    //监听pagesize改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getcompanylist()
    },
    //监听页码值改变的数据
    handleCurrentChange(newPage) {
      //console.log(newPage);
      this.queryInfo.pagenum = newPage
      this.getcompanylist()
    },
    //监听添加用户对话框的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields()
    },
    //点击按钮，添加新用户
    addcompany() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) {
          return
        }
        const { data: res } = await this.$http.post('regcompany', this.addForm)
        if (res.meta.status != 201) {
          this.$message.error(res.meta.msg)
        }
        this.$message.success(res.meta.msg)
        //隐藏
        this.addDialogVisible = false
        this.getcompanylist()
      })
    },

    //展示用户编辑的对话框
    async showEditDialog(id) {
      //console.log(id);
      const { data: res } = await this.$http.get('company/' + id)
      if (res.meta.status != 200) {
        return this.$message.error('获取信息错误')
      }
      this.editForm = res.data
      this.editDialogVisible = true
    },
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    //修改用户信息
    editcompanyInfo() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return
        //发起请求
        const { data: res } = await this.$http.put(
          'company/' + this.editForm.id,
          this.editForm
        )
        if (res.meta.status != 200) {
          this.$message.error('修改用户信息失败')
        }
        this.editDialogVisible = false
        this.getcompanylist()
        this.$message.success('修改用户信息成功')
      })
    },
    //删除用户
    async removeUserById(id) {
      //弹窗
      const confirmResult = await this.$confirm(
        '此操作将永久删除该用户, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
      //如果用户确定删除则返回字符串 confirm
      //如果用户取消 则cancel
      if (confirmResult != 'confirm') {
        return this.$message.info('已经取消删除')
      }
      console.log('确认删除')
      const { data: res } = await this.$http.delete('company/' + id)
      if (res.meta.status != 200) {
        return this.$message.error('删除用户失败')
      }
      this.$message.success('删除用户成功')
      this.getcompanylist()
    },
  },
}
</script>

<style lang="less" scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>